/* $RCSFile: ADNCRMPrincipal.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Security/ADNCRMPrincipal.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:30  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Security;
using System.Security.Principal;
using System.Web;
using System.Globalization;

namespace AddonNice.Security
{
   	#region Base TraceSwitch classe that define some usefull properties
	public class ADNTraceSwitch1 : TraceSwitch
	{
        public ADNTraceSwitch1()
            : base("DefaultTrace1","BAse Trace Switch for Security methods")
        { }
        public ADNTraceSwitch1(string nm, string des)
            : base(nm, des)
        { }
        public string Info{ get{ return string.Format("{0}-{1}",DisplayName,Level);}}
	}
	#endregion Base TraceSwitch classe that define some usefull properties

    #region TraceSwitch for All Security functions
    public class SecurityTraceSwitch 
	{
		static public ADNTraceSwitch1	Sw	=	new ADNTraceSwitch1("SecurityTrace","Trace Switch for all Security related functions");
	}
	#endregion TraceSwitch for All Security functions

	#region Authentication Trace switch
	public class AuthenticationTrace 
	{
		static public ADNTraceSwitch1	Sw	=	new ADNTraceSwitch1("AuthenticationTrace","Trace Switch for Authentication");
	}
	#endregion Authentication Trace switch
	
	/// <summary>
	/// ADNCRMPrincipal will replace WindowsPrincipal, inherits from GenericPrincipal and manage an AddonNiceIdentity for its IIdentity property
	/// </summary>
	public class ADNCRMPrincipal : GenericPrincipal
	{
        public IPrincipal oldPrincipal   =   null;
		
        /// <summary>
		/// GenericPrincipal dedicated for AddonNice wich store the old principal it will replace
		/// </summary>
        public ADNCRMPrincipal(IPrincipal woldPrincipal,IIdentity identity, string[] roles)
            : base(identity, roles)
		{
			if ( SecurityTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNCRMPrincipal constructor IIdentity Name: {0}, IsAuthenticated: {1}, oldPrincipal: {2}", 
                    identity.Name,identity.IsAuthenticated,oldPrincipal),SecurityTraceSwitch.Sw.Info);
            oldPrincipal    =   woldPrincipal;
		}
        /// <summary>
		/// GenericPrincipal dedicated for AddonNice
		/// </summary>
        public ADNCRMPrincipal(IIdentity identity, string[] roles)
            : base(identity, roles)
		{
			if ( SecurityTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNCRMPrincipal Ctor IIdentity Name: {0}, IsAuthenticated: {1}", identity.Name,identity.IsAuthenticated),SecurityTraceSwitch.Sw.Info);
		}


		/// <summary>
		/// Get the ADNCRM User
		/// </summary>
		public override IIdentity Identity 
		{
			get
			{
				AddonNiceIdentity Aid	=   null;
				if ( base.Identity is AddonNice.Security.AddonNiceIdentity )
				{
					Aid	                =	(AddonNiceIdentity)base.Identity;
                    if ( SecurityTraceSwitch.Sw.TraceVerbose )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNCRMPrincipal Identity GET, Identity is AddonNiceIdentity Name: {0}, CrmId: {1}, CrmImpersonateId: {2}, CrmImpersonateDomain: {3}, CrmImpersonateLogin: {4}.", 
                            new object[] { Aid.Name, Aid.CrmId, Aid.CrmImpersonateId, Aid.CrmImpersonateDomain, Aid.CrmImpersonateLogin }),SecurityTraceSwitch.Sw.Info);
					return Aid;
				}

				string msg              =   string.Format("[E]ADNCRMPrincipal Identity is not AddonNiceIdentity, Name: {0}, IsAuthenticated: {1}", (base.Identity.Name != null) ?base.Identity.Name:string.Empty, base.Identity.IsAuthenticated);
				if ( SecurityTraceSwitch.Sw.TraceError )
                    Trace.WriteLine(msg,SecurityTraceSwitch.Sw.Info);
				throw new Exception(msg);
			}
		}

		/// <summary>
		/// wrapper to avoid casting around the Identity property
		/// </summary>
		public AddonNiceIdentity AddonNiceIdentity 
		{
			get
			{
				if ( SecurityTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNCRMPrincipal AddonNiceIdentity Identity Name: {0}, Identity.IsAuthenticated: {1}", Identity.Name,Identity.IsAuthenticated),SecurityTraceSwitch.Sw.Info);
				return (AddonNiceIdentity)base.Identity;
			}
		}
	}
}